﻿using MyabpDDD.Demo.Application.FuelmanagementtableLY.Dto;
using MyCompany.MyProject.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace MyabpDDD.Demo.Application.FuelmanagementtableLY
{
    public class FuelmanagementtableLyService : ApplicationService, IFuelmanagementtableLyService
    {
        private readonly IRepository<Fuelmanagementtable> _repository;
        private readonly IRepository<Agenttable> _agenttables;
        public FuelmanagementtableLyService(IRepository<Fuelmanagementtable> students, IRepository<Agenttable> classes)
        {
            this._repository = students;
            this._agenttables = classes;
        }

        //public async Task<bool> Delete(int id)
        //{
        //    var model=await _repository.GetAsync(d=>d.Id==id);
        //    if (model != null)
        //    {
        //        _repository.DeleteAsync(model);
        //    }
        //    return true;
            
        //}

        public async Task<Fuelmanagementtable> Inser(FuelmanagementtableLyDto dto)
        {
            //Fuelmanagementtable yuh = new Fuelmanagementtable();
            //var s=ObjectMapper.Map<FuelmanagementtableLyDto, Fuelmanagementtable>(dto);

            //yuh.Licenseplate=dto.Licenseplate;
            //yuh.Comeonost=dto.Comeonost;
            //yuh.Amount=dto.Amount;
            //yuh.Mileage=dto.Mileage;成了 
            //yuh.Payment=dto.Payment;
            //yuh.Agent=dto.Agent;这次是代码有错了
            //yuh.Note=dto.Note;上面的报错没影响吗
            //yuh.Creationtime=dto.Creationtime;z
            //return await  _repository.InsertAsync(s);
            //var q2 = _agenttables.AsQueryable();
            //var yu=q2.Where(x=>x.AgenttaName==dto.AgenttaName).ToList();
            ////if (yu.Count() == 0)
            ////{
            //    AgenttableDto a =new AgenttableDto();
            //    a.AgenttaName=dto.AgenttaName;
            //Agenttable yu78 = ObjectMapper.Map<AgenttableDto, Agenttable>(a);
            //await _agenttables.InsertAsync(yu78);
            
            //var q22 = _agenttables.AsQueryable();
            //    var yu2 = q22.Where(x => x.AgenttaName == dto.AgenttaName).ToList();
            //    dto.Agent = yu2[0].Id;
            //}
            
            Fuelmanagementtable www=ObjectMapper.Map<FuelmanagementtableLyDto, Fuelmanagementtable>(dto);
            return await _repository.InsertAsync(www);
        }

      

        public IList<FuelmanagementtableLyDto> QueryStudentAll()
        {
            var q1 = _repository.AsQueryable();
            var q2 = _agenttables.AsQueryable();
            var query = from a in q1
                        join a2 in q2 on a.Agent equals a2.Id
                        select new FuelmanagementtableLyDto()
                        {
                            Licenseplate=a.Licenseplate,
                            Comeonost=a.Comeonost,
                            Amount=a.Amount,
                            Mileage=a.Mileage,
                            Payment=a.Payment,
                            Agent=a.Agent,
                            Note=a.Note,
                            Creationtime=a.Creationtime,
                           // AgenttaName=a2.AgenttaName
                        };
            return query.ToList();
        }

        public async Task<bool> update(FuelmanagementtableLyDto dto)
        {
            //Fuelmanagementtable s = ObjectMapper.Map<FuelmanagementtableLyDto, Fuelmanagementtable>(dto);
            Fuelmanagementtable yuh = new Fuelmanagementtable();
            yuh.Licenseplate = dto.Licenseplate;
            yuh.Comeonost = dto.Comeonost;
            yuh.Amount = dto.Amount;
            yuh.Mileage = dto.Mileage;
            yuh.Payment = dto.Payment;
            yuh.Agent = dto.Agent;
            yuh.Note = dto.Note;
            yuh.Creationtime = dto.Creationtime;
            var db = _repository.GetDbSet<Fuelmanagementtable>();
            db.Update(yuh);
            return true;
        }
    }
}
